<?php
set_time_limit ( 1800 );
$PARAM_hote = 'localhost';
$PARAM_utilisateur = 'root';
$PARAM_mot_passe = '';
$PARAM_nom_bd = 'serveurlicence';
$connexion = new PDO ( 'mysql:host=' . $PARAM_hote . ';dbname=' . $PARAM_nom_bd, $PARAM_utilisateur, $PARAM_mot_passe );
$soap = new SoapClient ( "https://www.ovh.com/soapi/soapi-re-1.63.wsdl" );
// login
$session = $soap->login ( "df44485-ovh", "53aCt5Se", "fr", false );
$vretour = array ();
$value = "";
$requete = 'SELECT id, nom, prenom FROM praticiens';
$requeteInsert = 'INSERT INTO gestsmsclients VALUES (null,:id, :Nom, :Prenom, :DateMAJ, :MoisConcerne, :NbSMSMoisConcerne)';
/* renvoie du rÃ©sultat de la requÃªte */
$result = $connexion->prepare ( $requete );
$insertion = $connexion->prepare ( $requeteInsert );
$nbSmsPratMoisPrec = 0;
$nbSmsPratMoisActu = 0;
	$result->execute ();
	echo $i."<br>";
	$moisConcerne = date ( 'Y-m-d', mktime ( 0, 0, 0, date ( 'm' ) - 0, date ( 'd' ), date ( 'Y' ) ) );
	if (! $result) {
		echo '<tr>Aucune licence trouvÃ©e</tr>';
	} else {
		while ( $data = $result->fetch ( PDO::FETCH_OBJ ) ) {
			$nbSmsPratMoisPrec = nbSmsPrat ( $data->id, 0, $soap, $session );
			// $nbSmsPratMoisActu = nbSmsPrat ( $data->id, "courant", $soap, $session );
			if ($nbSmsPratMoisPrec != 0) {
				echo date ( 'Y-m-d' ) . "\n";
				echo date ( 'Y-m-d', mktime ( 0, 0, 0, date ( 'm' ) - 1, date ( 'd' ), date ( 'Y' ) ) ) . "\n";
				echo $data->id . "\n";
				echo $data->nom . "\n";
				echo $data->prenom . "\n";
				echo $nbSmsPratMoisPrec . "\n";
				echo $insertion->execute ( array (
						":id" => $data->id,
						":Nom" => $data->nom,
						":Prenom" => $data->prenom,
						":DateMAJ" => date ( 'Y-m-d', mktime ( 0, 0, 0, date ( 'm' ), date ( 'd' ), date ( 'Y' ) ) ),
						":MoisConcerne" => $moisConcerne,
						":NbSMSMoisConcerne" => $nbSmsPratMoisPrec 
				) );
				$insertion->closeCursor();
			}
		}
	}
	$result->closeCursor ();
$soap->logout ( $session );
function nbSmsPrat($id, $i, $soap, $session) {
	$nbsms = 0;
	echo $i."<br>";
	$moisdernier = date ( 'Ym', mktime ( 0, 0, 0, date ( 'm' ) - $i, date ( 'd' ), date ( 'Y' ) ) );
	try {
		
		// echo "login successfull\n";
		$return = array ();
		// telephonySmsHistory
		$result = $soap->telephonySmsHistory ( $session, "sms-df44485-1", "", "$id", "", "", "", "", "", "" );
		// echo "telephonySmsHistory successfull\n";
		$size = count ( $result );
		for($i = 0; $i < $size; $i ++) {
			if (substr ( $result [$i]->date, 0, 6 ) == $moisdernier) {
				$nbsms ++;
			}
		}
		return $nbsms;
		
		// return ($i); // your code here ...
		
		// logout
		
		// echo "logout successfull\n";
	} catch ( SoapFault $fault ) {
		// echo $fault;
		return 'Erreur';
	}
}
function utf8_encode_array($array) {
	return utf8_encode ( $array );
}
?>